/*
 * jQuery UI Feeds v0.1
 *
 * Copyright (c) 2009 AUTHORS.txt (http://jqueryui.com/about)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://mjonrails.blogspot.com/
 *
 * Depends:
 *   ui.core.js
 *   javascript.extensions.js
 */
(function($) {

$.widget("ui.rotator", {
	
	_init: function() {
		$(this).data('selectedIndex',  1);
		
		var options = this.options;
		var context = this;
		setInterval(function (){
				if(context._getData("autoRotate")){
					context.scrollFeed(1);
				}
			}, 5000);
		
		
		$(".-ui-rotator-wrapper", this.element).bind('mousewheel', (function(context){
				
				return function (event, delta){
					var step = delta < 0 ? 1 : -1;
					context.scrollFeed(step);
					event.preventDefault();
				};
			}(this)));
		
		$(this.element)
			.bind("mouseenter",
					function(e){
						context._setData("autoRotate", false);
						context.showNavigation(context.element);
					}
			)
			.bind("mouseleave",
					function(e){
						context._setData("autoRotate", true);
						context.hideNavigation(context.element);
					}
					
			);
		
		$(".-ui-rotator-navigate", this.element).bind("click",(function(context){
			return function(e){
				var wrapper = $(".-ui-rotator-wrapper",$(this).parent());
				if($(this).hasClass("previous")){
					context.scrollFeed(-1);
					return;
				}
				if($(this).hasClass("next")){
					context.scrollFeed(1);
					return;
				}
			};
		}(this)));
		
	},
	
	feedCount : function(){
		return $(".-ui-rotator-feed" , this.element).size();
	},
	
	getFeedByIndex: function(idx){
		var feeds = $(".-ui-rotator-feed" , this.element);
		return feeds[idx -1];	
	},
	
	setDataSource: function(dataSource){
		this._setData("dataSource", dataSource);
		
	},
	
	getDataSource: function(){
	},
	
	scrollFeed: function(step){
		var newIndex = this._getData('selectedIndex') + step;
		var feedSize = this.feedCount();
		
		if ( feedSize < newIndex ){
			newIndex = 1;
		}
		if ( newIndex <=0 ){
			newIndex = feedSize;
		}
		
		this._setData('selectedIndex',newIndex);
		$(".-ui-rotator-wrapper",this.element).animate({scrollTop:140*(newIndex -1)}, 800);
		
	},
	
	showNavigation: function(context){
		$(".-ui-rotator-navigate",context).fadeIn(300);
	},
	
	hideNavigation: function(context){
		$(".-ui-rotator-navigate",context).fadeOut(300);
	},
	
	
	destroy: function() {
		
		$.widget.prototype.destroy.apply(this, arguments);
	}

});

$.extend($.ui.rotator, {
	version: "0.1.0",
	defaults: {
		/** format options
		 * {0} - "http://example.com/link/to/article.html"
		 * {1} - "more details" 
		 * {2} - "..."  
		 */
		ellipsisTemplate : '<a href="{0}" class="-ui-rotator-detailsLink" title="{1}">{2}</a>', 
		feeds : [{ title: "Thank You", html : "", detailsUrl : "#" }, { title: "How to use it?", html : "", detailsUrl : "#" }],
		/** format options
		 * {0} - feeds[idx].title
		 * {1} - feeds[idx].html
		 * {2} - elipsisTemplate.format(feeds[idx].detailsUrl, "more details", "...");  
		 */
		feedItemTemplate: '<div class="-ui-rotator-feed"><h4>{0}</h4><p>{1}{2}</p></div>',
		selectedIndex : 1,
		interval: 3000,
		autoRotate: true
	}
});

})(jQuery);



